package com.imooc.modular.controller;

import com.imooc.modular.UserNotExistException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * 异常拦截类：自定义异常处理
 *
 * @author yuelinbo
 * @date 2019/2/9 21:22
 **/
@ControllerAdvice
public class ControllerExceptionHandler {


    @ExceptionHandler(UserNotExistException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String,Object> handlerUserNotExistException(UserNotExistException ex){

        Map<String,Object> result = new HashMap<>(16);
        result.put("id",ex.getId());
        result.put("message",ex.getMessage());
        return null;
    }
}
